/**
 * bootstrap datatimepicker plugins
 */
(function() {
	'use strick',
	require.config({
		paths: {
			"bootstrap.daterange": "bootstrap/extends/bootstrap-daterangepicker/2.1.17/js/daterangepicker.min"
		},
		shim: {
			"bootstrap.daterange": {
				deps : [
				   "moment", 
				   "jquery", 
				   "css!bootstrap/extends/bootstrap-daterangepicker/2.1.17/css/daterangepicker.min.css"
				],
				exports:"jQuery.fn.daterangepicker"
			}
		}
	});
	
	define(['moment','bootstrap.daterange'], function(moment){
		return {
			register:function(elem, options){
				var _opts = jQuery.extend({
					locale: {
						"customRangeLabel": "自选",
						"cancelLabel": "取消",
						"applyLabel": "确认",
						"separator": " ~ ",
						"fromLabel": "从",
						"toLabel": "到",
				        "format": "YYYY-MM-DD",
				        "firstDay": 1,
				        "daysOfWeek": [ "日", "一", "二", "三",  "四", "五", "六" ],
				        "monthNames": ["一月", "二月", "三月", "四月", "五月",  "六月", "七月", "八月", "九月",  "十月", "十一月",  "十二月" ]
				    },
				    ranges:{
				    	"全年":getYear("full"),
				    	"半年":getYear("half")
				    },
				    showWeekNumbers:true,
				    autoUpdateInput:true
				}, options);
		  
				jQuery(elem).daterangepicker(_opts, function(start, end, label){
					var intput = jQuery(this.element[0]);
					var target = intput.data("input-target");
					var start = start.format('YYYY-MM-DD');
					var end = end.format('YYYY-MM-DD');
					jQuery(target, intput).val(start+" ~ "+end);
					jQuery(".fromDate", intput).val(start);
					jQuery(".overDate", intput).val(end);
				});
				
				function getYear(tag){
					var date = moment();
					var month = date.month();
					switch(tag){
						case "full":
						return [date.format('YYYY-MM-DD'), date.month(month+12).format('YYYY-MM-DD')];
						case "half":
						return [date.format('YYYY-MM-DD'), date.month(month+6).format('YYYY-MM-DD')];
						case "one":
						return [];
						case "two":
						return [];
					}
				}
			}
		}
	});
})(this);
